package com.roomwrod.fragment;


import android.content.Context;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProviders;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.roomwrod.R;
import com.roomwrod.livedata.viewmodel.WordViewModel;
import com.roomwrod.model.Word;

/**
 * A simple {@link Fragment} subclass.
 */
public class AddFragment extends Fragment {

    private Button btn_add;
    private EditText et_english, et_chinese;
    WordViewModel viewModel;
    private TextWatcher watcher;

    public AddFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_add, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        viewModel = ViewModelProviders.of(requireActivity()).get(WordViewModel.class);
        et_english = requireActivity().findViewById(R.id.et_english);
        et_chinese = requireActivity().findViewById(R.id.et_chinese);
        btn_add = requireActivity().findViewById(R.id.btn_add);
        btn_add.setEnabled(false);
        et_english.requestFocus();
        InputMethodManager imm = (InputMethodManager) requireActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(et_english, 0);

        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String english = et_english.getText().toString().trim();
                String chinese = et_chinese.getText().toString().trim();
                Word word = new Word(english, chinese, false);
                viewModel.insert(word);
                //返回 关闭当前fragment页面
                NavController controller = Navigation.findNavController(view);
                controller.navigateUp();
                InputMethodManager imm = (InputMethodManager) requireActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
            }
        });
        watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String english = et_english.getText().toString().trim();
                String chinese = et_chinese.getText().toString().trim();
                btn_add.setEnabled(!english.isEmpty() && !chinese.isEmpty());
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        };

        et_english.addTextChangedListener(watcher);
        et_chinese.addTextChangedListener(watcher);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (watcher != null) {
            watcher = null;
        }
    }


}








